package executors;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import tweeter.LogParser;
import tweeter.NetworkEntity;
import tweeter.Tweet;

public class DummyExecutor
{
	public static void main(String[] args) throws FileNotFoundException, ParseException 
	{
		Map<Integer, NetworkEntity> entities = new HashMap<Integer, NetworkEntity>();
		System.out.println("Starting to generate the entities!");
		Set<Integer> entityIDs = LogParser.generateAllEntityIDs("feeds/followers/9", "feeds/tweets/9");
		System.out.println("Done...");
		System.out.println(entityIDs);
		System.out.println(entityIDs.size());

		Iterator<Integer> entityIterator = entityIDs.iterator();
		while (entityIterator.hasNext())
		{
			Integer ID = entityIterator.next();
			NetworkEntity entity = new NetworkEntity(ID);
			entities.put(ID, entity);
		}
		System.out.println("Starting to generate subscriptions!");		
		Map<Integer, HashSet<Integer>> subscriptions = LogParser.generateSubscriptions("feeds/followers/9");
		System.out.println("Done...");
		System.out.println(subscriptions);
		System.out.println(subscriptions.size());

		for (Integer ID : subscriptions.keySet())
		{
			NetworkEntity entity = entities.get(ID);
			entityIterator = subscriptions.get(ID).iterator();
			while (entityIterator.hasNext())
			{
				entity.subscribe(entityIterator.next());
				entityIterator.next();
			}
			
		}
		
		System.out.println("Starting to generate tweets!");
		ArrayList<Tweet> tweets = LogParser.generateTweets("feeds/tweets/9");
		System.out.println("Done...");

		System.out.println(tweets.size());
		Random generator = new Random();
		int startingPoint = generator.nextInt(tweets.size());
		System.out.println(startingPoint);
		while (((tweets.size() - startingPoint) < 5000) || ((Integer)5000 > startingPoint))
		{
			startingPoint = generator.nextInt(tweets.size());
			System.out.println(startingPoint);
		}
		
		System.out.println("Starting from " + startingPoint);
		int runningCounter = startingPoint;
		int messageIDs = 0;
		Tweet startingTweet = null;

		startingTweet = tweets.get(startingPoint);
				
		Calendar upperBound = (Calendar) startingTweet.tweetTimestamp.clone();
		upperBound.add(Calendar.HOUR, 2);

		while (runningCounter < tweets.size() && tweets.get(runningCounter).tweetTimestamp.before(upperBound))
		{
			Tweet currentTweet = tweets.get(runningCounter++);
			entities.get(currentTweet.tweeterID).publish(messageIDs++, currentTweet.tweetData);
		}
	}
}
